﻿/*****************************************************************************
Copyright (c) 2011 SmartVault, Inc.

 Permission is hereby granted, free of charge, to any person obtaining a copy
 of this software and associated documentation files (the "Software"), to deal
 in the Software without restriction, including without limitation the rights
 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 copies of the Software, and to permit persons to whom the Software is
 furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in
 all copies or substantial portions of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 THE SOFTWARE.
******************************************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using SmartVault.OAuth.Serializer;

namespace SmartVault.OAuth.Interfaces
{
    public class AuthScheme
    {
        private string _scheme;
        private static Dictionary<string, AuthScheme> _entries = new Dictionary<string, AuthScheme>();

        private AuthScheme(string scheme)
        {
            _scheme = scheme;
            _entries.Add(scheme, this);
        }

        public static explicit operator AuthScheme(string tmp)
        {
            return _entries[tmp];
        }

        public static explicit operator string(AuthScheme tmp)
        {
            return tmp.ToString();
        }

        public static bool operator ==(AuthScheme lhs, AuthScheme rhs)
        {
            if (((object)lhs) == null) return ((object)rhs) == null;
            return lhs.Equals(rhs);
        }

        public static bool operator !=(AuthScheme lhs, AuthScheme rhs)
        {
            return !(lhs == rhs);
        }

        public override bool Equals(object obj)
        {
            AuthScheme fac = obj as AuthScheme;
            return fac == null ? false : this.ToString() == fac.ToString();
        }

        public override int GetHashCode()
        {
            return _scheme.GetHashCode();
        }

        public override string ToString()
        {
            return _scheme;
        }

        public static AuthScheme GetSchemeFromToken(byte[] data)
        {
            ByteReader reader = new ByteReader(data);
            return (AuthScheme)reader.Read(5);
        }

        public static AuthScheme GetSchemeFromToken(string data)
        {
            return GetSchemeFromToken(Convert.FromBase64String(data));
        }

        public static AuthScheme SelfToken = new AuthScheme("SLF00");
        public static AuthScheme ClientToken = new AuthScheme("CLI00");
        public static AuthScheme DelegationToken = new AuthScheme("DEL00");
        public static AuthScheme PasswordToken = new AuthScheme("PAS00");

        public static AuthScheme UserPinToken = new AuthScheme("UPN00");

        //public static AuthScheme DelegationToken2 = new AuthScheme("DEL01");

        /// <summary>
        /// Public Client Token
        /// 
        /// This is provided by a client incapable of protecting a private key.
        /// </summary>
        //public static AuthScheme PublicClientToken = new AuthScheme("PLI00");
    }
}
